package com.etoak.common.jwt;


import io.jsonwebtoken.Jwts;
import io.jsonwebtoken.security.Keys;

import java.security.Key;
import java.util.Date;
import java.util.Map;

public class MyJwtUtil {

    public static final String SECRET_STRING = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa";

    private static final Key KEY = Keys.hmacShaKeyFor(SECRET_STRING.getBytes());

    public static final long EXPIRE = 60 * 60 * 12 * 1000;

    public static String create(Map<String, Object> claims) {
        // 签发时间
        Date now = new Date();
        return Jwts.builder()
                .signWith(KEY)
                .setClaims(claims)
                .setIssuedAt(now)
                .setExpiration(new Date(now.getTime() + EXPIRE))
                .compact();
    }

    public static Map<String, Object> parse(String token) {
        return Jwts.parserBuilder()
                .setSigningKey(KEY)
                .build()
                .parseClaimsJws(token)
                .getBody();
    }
}
